import axios from '../../util/http'

const good = {
  state:{
    goodInfo:null,
    swiperList:[],
    totalStock:0,
    goodsSpecList:[]
  },
  mutations: {
    SET_goodInfo:(state,data)=>{
      state.goodInfo = data
    },
    SET_swiperList:(state,data)=>{
      state.swiperList = data
    },
    SET_totalStock:(state,data)=>{
      state.totalStock = data
    },
    SET_goodsSpecList:(state,data)=>{
      state.goodsSpecList = data
    },
  },
  actions:{
    queryGetGoodInfo:({commit,rootState},goodsId)=>{
      return new Promise((resolve,reject)=>{
        axios.post(rootState.getGoodInfo,{goodsId:goodsId}).then(res=>{
          console.log(res.data)
          let {goodsSpecList} =res.data
          let stock = 0,
              swiperList = [];
          goodsSpecList.forEach(v=>{
            stock += v.stock
            swiperList.push(v.specImgUrl)
          })
          commit('SET_goodInfo',res.data)
          commit('SET_swiperList',swiperList)
          commit('SET_totalStock',stock)
          commit('SET_goodsSpecList',goodsSpecList)


          resolve(res.data)
        }).catch(e=>{
          reject(e)
        })
      })
    }
  }
}

export default good
